#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#define NOISY_TEST 0

void testAdventurer(){
	int player, preHandCount, handCount;
	int preDeckCount, deckCount;
	int prePlayedCount, playedCount;
	int preDiscardCount, discardCount;
	int seed = 50;
	int numPlayer = 3;
	struct gameState state;
	int cur, it;
	int k[10] = {adventurer, council_room, feast, gardens, mine,
					remodel, smithy, village, baron, great_hall};


	// set choices to playDom example
	int choice1 = 0, choice2 = 0, choice3 = 0, handPos = 0, bonus=0;

	for(player = 0; player < numPlayer; player++){

		// initialize game
	   	int r = initializeGame(numPlayer, k, seed, &state);
		if(r < 0){
	    	printf("Error initializing game...\n");
		}

		// set the player
		state.whoseTurn = player;
		// compensate for initializeGame where other players don't draw their cards
		if(player > 0){
			for(it = 0; it < 5; it++){
                 drawCard(state.whoseTurn, &state);
			}
		}

		// verify empty hand
		if(state.handCount[player] == 0){
	         state.handCount[player] = 1;
	#if (NOISY_TEST == 1)
			printf("Player %d handcount was empty, new handcount: %d\n", player, state.handCount[player]);
	#endif
		}

		// force adventurer card to be the first card in hand
		state.hand[player][0] = adventurer;		

		// Determine preconditions
		preHandCount = state.handCount[player];
		preDeckCount = state.deckCount[player];
		prePlayedCount = state.playedCardCount; // discard pile
		preDiscardCount = state.discardCount[player]; // different type of discard

		cur = cardEffect(adventurer, choice1, choice2, choice3, &state, handPos, &bonus);
	    if(cur != 0){
	        printf("ERROR: Adventurer card could not be used.\n");
			break;
		}                                                        
		// obtain postconditions
		handCount = state.handCount[player];
		deckCount = state.deckCount[player];
		playedCount = state.playedCardCount;
		discardCount = state.discardCount[player];

			
	#if (NOISY_TEST == 1)
		// Adventurer Card should add 2 more cards to the hand
        printf("Player %d Hand Count - Before: %d Expected: %d  Actual: %d\n", player, preHandCount, preHandCount + 2, handCount);
	#endif
		assert(preHandCount + 2 == handCount);

		// Adventurer Card will lose 2 cards from deck to hand and any # of discards.
	#if (NOISY_TEST == 1)
	    printf("Player %d Deck Count - Before: %d After: %d\n", player, preDeckCount, deckCount);
		printf("Player %d Discard Count - Before: %d After: %d\n", player, preDiscardCount, discardCount);
		printf("Player %d Deck Sanity - Expected %d Actual: %d\n", player, preDeckCount, deckCount + discardCount + 2);
	#endif
		assert(deckCount + 2 + discardCount == preDeckCount);

	}
	printf("Finishing testAdventurer()\n");
}

int main(){
	testAdventurer();
	return 0;
}
